package com.yjs.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.yjs.bean.Goods;
import com.yjs.bean.Store;
import com.yjs.service.GoodsService;
import com.yjs.service.StoreService;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 货物控制层
 *
 * @author yjs
 * @create 2020-09-17-10:06
 */
@Controller
public class GoodsController {

    //注入货物service层
    @Autowired
    GoodsService goodsService;
    //注入仓储service层
    @Autowired
    StoreService storeService;

    //查询所有的仓储信息
    @GetMapping("/goods")
    public String getAllGoods(Model model){
        List<Goods> allGoods = goodsService.getAllGoods();
        model.addAttribute("goods",allGoods);
        return "goods/good";
    }
    //跳转到货物新增页面
    @GetMapping("/good")
    public String toAddPage(Model model){
        List<Store> allStore = storeService.getAllStore();
        model.addAttribute("stores",allStore);
        return "goods/add";
    }
    //处理新增货物请求
    @PostMapping("/good/add")
    public String addGood(Goods goods,HttpSession session){
        String username = (String) session.getAttribute("loginUser");
        goodsService.addGoods(goods,username);
        return "redirect:/goods";
    }
    //处理删除货物请求
    @DeleteMapping("/good/del/{gid}")
    public String toDel(@PathVariable("gid") String gid,HttpSession session){
        String username = (String) session.getAttribute("loginUser");
        goodsService.delGoods(gid,username);
        return "redirect:/goods";
    }
    //处理修改前查询的货物请求
    @GetMapping("/good/upd/{gid}")
    public String toUpdatePage(@PathVariable("gid") String gid,Model model){
        Goods oneGoods = goodsService.getOneGoods(gid);
        model.addAttribute("oneGoods",oneGoods);

        List<Store> allStore = storeService.getAllStore();
        model.addAttribute("stores",allStore);
        return "goods/add";
    }
    //处理修改货物请求
    @PutMapping("/good/add")
    public String updGoods(Goods goods,HttpSession session){
        String username = (String) session.getAttribute("loginUser");
        goodsService.updGoods(goods,username);
        return "redirect:/goods";
    }



}
